package com.wangsd.web.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wangsd.web.coupon.entity.ActivityProduct;
import com.wangsd.web.coupon.entity.ActivityProduct;
import com.wangsd.web.coupon.entity.ActivityProductRelation;
import com.wangsd.web.coupon.mapper.ActivityProductMapper;
import com.wangsd.web.coupon.mapper.ActivityProductMapper;
import com.wangsd.web.coupon.mapper.ActivityProductRelationMapper;
import com.wangsd.web.coupon.service.ActivityProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wangsd.web.coupon.service.ActivityProductRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author generator
 * @since 2019-10-23
 */
@Service
public class ActivityProductServiceImpl extends ServiceImpl<ActivityProductMapper, ActivityProduct> implements ActivityProductService {

    @Resource
    ActivityProductMapper activityProductMapper;

    @Resource
    ActivityProductRelationMapper activityProductRelationMapper;

    @Autowired
    ActivityProductRelationService activityProductRelationService;

    @Override
    public IPage<ActivityProduct> selectPage(String code, Integer pageNum, Integer pageSize, ActivityProduct activityProduct) {
        Page<ActivityProduct> page = new Page<>(pageNum, pageSize);
        return activityProductMapper.selectListXml(page, code, activityProduct);
    }

    @Override
    public int addOrUpdate(ActivityProduct entity) {
        if(entity.getId()==null){
            activityProductMapper.insert(entity);
            List<ActivityProductRelation> productRelations = new ArrayList<>();

            /*保存关联活动和商品的关联关系*/
            entity.getProductIds().forEach(productId->{
                ActivityProductRelation activityProductRelation = new ActivityProductRelation();
                activityProductRelation.setActivityProductId(entity.getId());
                activityProductRelation.setObjectId(productId);
                productRelations.add(activityProductRelation);
            });
            activityProductRelationService.saveBatch(productRelations);
        }else{
            /*修改*/
            activityProductMapper.updateById(entity);
            /*删除之前活动和商品关联关系数据*/
            activityProductRelationMapper.delete(new QueryWrapper<ActivityProductRelation>().eq("activity_product_id",entity.getId()));

            /*更新新的关联关系*/
            List<ActivityProductRelation> productRelations = new ArrayList<>();
            entity.getProductIds().forEach(productId->{
                ActivityProductRelation activityProductRelation = new ActivityProductRelation();
                activityProductRelation.setObjectId(productId);
                activityProductRelation.setActivityProductId(entity.getId());
                productRelations.add(activityProductRelation);
            });
            activityProductRelationService.saveBatch(productRelations);
        }
        return 1;
    }

    @Override
    public int upActivity(ActivityProduct activityProduct) {
        /*上架参加当前活动商品时，需将该商圈下的其他活动置为下架状态*/
        if(activityProduct.getStatus()==1){
            ActivityProduct byId = activityProductMapper.selectById(activityProduct.getId());
            Long companyId = byId.getCompanyId();
            activityProductMapper.updateStatusByCompanyId(companyId,0);
        }
        /*更新当前商品状态为状态*/
        this.updateStatus(activityProduct.getId(),activityProduct.getStatus());
        return 1;
    }

    @Override
    public int updateStatus(Long id, Integer status) {
        ActivityProduct activityProduct = new ActivityProduct();
        activityProduct.setStatus(status);
        activityProduct.setId(id);
        activityProductMapper.updateById(activityProduct);
        return 0;
    }

    @Override
    public ActivityProduct selectListByCompanyId(Long companyId) {
        ActivityProduct activityProduct = new ActivityProduct();
        activityProduct.setCompanyId(companyId);
        List<ActivityProduct> activityProducts = activityProductMapper.selectListByCompanyId(activityProduct);
        ActivityProduct result = activityProductMapper.selectOne(new QueryWrapper<ActivityProduct>().eq("company_id", companyId)
        .eq("status",1));
        result.setActivityProducts(activityProducts);
        return result;
    }

    @Override
    public List<String> selectActivityProductByCompanyId(Long id) {
        ActivityProduct activityProduct = new ActivityProduct();
        List<String> collect = null;
        activityProduct.setId(id);
        List<ActivityProductRelation> list = activityProductRelationMapper.selectActivityProductByCompanyId(activityProduct);
        if(list!=null&&list.size()>0){
            collect = list.stream().map(ActivityProductRelation::getObjectId).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    public List<String> selectActivityProductProductByCompanyId(Long companyId) {
        ActivityProduct activityProduct = new ActivityProduct();
        activityProduct.setCompanyId(companyId);
        activityProduct.setBeginTime(new Date());
        List<ActivityProductRelation> list = activityProductRelationMapper.selectActivityProductByCompanyId(activityProduct);
        List<String> collect = null;
        if(list!=null&&list.size()>0){
            collect = list.stream().map(ActivityProductRelation::getObjectId).collect(Collectors.toList());
        }
        return collect;
    }

    @Override
    public ActivityProduct selectActivityProductByProductId(String objectId,Integer objectType) {
        ActivityProduct activityProduct = new ActivityProduct();
        activityProduct.setObjectId(objectId);
        List<ActivityProduct> activityProducts = activityProductMapper.selectListByCompanyId(activityProduct);
        if(activityProducts!=null&&activityProducts.size()>0){
            return activityProducts.get(0);
        }else {
            return null;
        }
    }
}
